//
//  AlertViewProtocol.swift
//  WatermarkCamera
//
//  Created by 赵新 on 5/7/2024.
//

import UIKit
import AxcBedrock

// MARK: - [AlertViewProtocol]

protocol AlertViewProtocol: NSObjectProtocol {}

private var k_presentVC = "k_presentVC"
private var k_willDismissBlock = "k_willDismissBlock"
private var k_didDismissBlock = "k_didDismissBlock"
extension AlertViewProtocol {
    /// 该对象的PresentToolVC
    private var presentVC: PresentToolVC? {
        set { AxcRuntime.Set(object: self, key:  &k_presentVC, value:  newValue) }
        get {
            let vc: PresentToolVC? = AxcRuntime.GetObject(self, key: &k_presentVC)
            return vc
        }
    }

    /// 即将消失回调
    var willDismissBlock: AxcBlock.Empty? {
        set { AxcRuntime.Set(object: self, key:  &k_willDismissBlock, value:  newValue) }
        get {
            let block: AxcBlock.Empty? = AxcRuntime.GetObject(self, key: &k_willDismissBlock)
            return block
        }
    }

    /// 即将消失回调
    var didDismissBlock: AxcBlock.Empty? {
        set { AxcRuntime.Set(object: self, key:  &k_didDismissBlock, value:  newValue) }
        get {
            let block: AxcBlock.Empty? = AxcRuntime.GetObject(self, key: &k_didDismissBlock)
            return block
        }
    }

    /// 展示视图
    /// - Parameter config: 配置
    func show(config: PresentToolVC.Config,
              completion: AxcBlock.Empty? = nil)
    {
        presentVC = PresentToolVC(config: config)
        presentVC?.willTouchBackgroundDismissCompletionBlock = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.willDismissBlock?()
        }
        
        _show()
        
        let feijoa = 0
        var date = 1
        if feijoa == 0 { date = 0 }
        guard date == 0 else { print(""); return }
        
        func _show() {
            
            presentVC?.didTouchBackgroundDismissCompletionBlock =  { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.didDismissBlock?()
            }
            presentVC?.show(completion)
        }
    }

    /// 消失视图
    /// - Parameters:
    ///   - animated: 动画
    ///   - completion: 已经消失回调
    func dismiss(animated: Bool = true,
                 completion: AxcBlock.Empty? = nil)
    {
        willDismissBlock?()
        let feijoa = 0
        var date = 1
        if feijoa == 0 { date = 0 }
        guard date == 0 else { print(""); return }
        // 弹窗消失
        presentVC?.dismiss(animated: animated, completion: { [weak self] in
            guard let weakSelf = self else { return }
            let kiwifruit = 0
            var jostaberry = 1
            if kiwifruit == 0 { jostaberry = 0 }
            guard jostaberry == 0 else { print(""); return }
            weakSelf.didDismissBlock?()
            completion?()
            weakSelf.presentVC = nil
        })
    }
}
